﻿$(function () {
    var right1 = Math.max.apply(null, $('line').map(function () {
        return parseFloat($(this).attr('x1'));
    }).get());
    var right2 = Math.max.apply(null, $('line').map(function () {
        return parseFloat($(this).attr('x2'));
    }).get());
    var right = right1 > right2 ? right1 : right2;
    var inside = false;
    $("g rect").each(function () {
        if (parseFloat($(this).attr('x')) > right) {
            var self = $(this);
            $(this).nextAll("text").first().mouseenter(function (e) {
                if (!inside) {
                    inside = true;
                    $(self).trigger(e.type);
                }
            }).mouseleave(function (e) {
                $(self).trigger(e.type);
                inside = false;
            });
            $(this).mouseenter(function () {
                var group = $(this).attr('g');
                var elements = $("g ellipse[g='" + group + "']");
                $(elements).each(function () {
                    if ($(this).css('fill') !== 'none') {
                        addHighlight($(this));
                    }
                });
            }).mouseleave(function () {
                $("ellipse[class='selected']").remove();
            });

        }
    });

    $("g ellipse, g text").each(function () {
        if ($(this).get(0).tagName === 'ellipse') {
            $(this).mouseenter(function () {
                if ($(this).css('fill') !== 'none') {
                    addHighlight($(this), false);
                } else {
                    if ($(this).next().css('stroke') !== 'none') {
                        addHighlight($(this));
                    }
                }
            }).mouseleave(function () {
                $("g ellipse[class='selected']").remove();
            });
        } else {
            var prev = $(this).prev();
            if ($(prev).attr('cx') === $(this).attr('x') && $(prev).get(0).tagName === 'ellipse') {
                $(this).mouseenter(function () {
                    addHighlight(prev);
                }).mouseleave(function () {
                    $("ellipse[class='selected']").remove();
                });
            }
        }
    });

    function addHighlight(element) {
        if ($(element).next().hasClass('selected') || $(element).attr('id') === undefined) {
            return;
        }
        var cloned = $(element).clone().removeAttr('id').removeAttr('opacity').css('fill', 'none').attr('class', 'selected');
        var rx = parseFloat(cloned.attr('rx'));
        var ry = parseFloat(cloned.attr('ry'));
        var opacity = ["0.45", "0.375", "0.3", "0.15", "0.05"];

        cloned.css('stroke', '#000000');
        cloned.css('stroke-width', 1);

        for (var i = 0; i < opacity.length; i++) {
            cloned = $(cloned).clone();
            cloned.css('opacity', 1);
            cloned.attr('rx', rx + i);
            cloned.attr('ry', ry + i);
            cloned.css('stroke-opacity', opacity[i]);
            cloned.insertAfter($(element));
        }
    }
});